#!/usr/bin/env node
const fs = require("fs")
const path = require("path")
// if(process.argv[2]&&process.argv[2]=== '--version'){
//     let packageJsonData = JSON.parse(fs.readFileSync(path.join(__dirname,'package.json')).toString())
//     console.log(packageJsonData)
// }
// console.log("欢迎")


//commander 的使用
const { program } = require('commander')
const superagent = require("superagent")
const urlencode = require("urlencode")
const inquirer = require("inquirer")
program.version('0.0.1')
program
    .option('-d, --debug', 'output extra debugging')
    .option('-s, --save', 'save file')
    .option('-f, --find', 'To find the history')
    .option('-c, --constellation','The constellation information')

program.parse(process.argv);
if(program.constellation){
    const prompList = [{
        type:"input",
        message:"请输入你要查询的星座",
        name:"constellation",
        default:"双鱼"
    }]
    inquirer.prompt(prompList).then(answers =>{
        let str = urlencode(answers.constellation)
        superagent.post("http://web.juhe.cn:8080/constellation/getAll?key=1e86cbe4ac7c8e1d23dcc5a133221f7b&type=today&consName="+str).then(res =>{
            console.log(JSON.parse(res.text))
        })
    })
    
}
if (program.debug) {
    let str = urlencode(process.argv[3])
    superagent.post('http://web.juhe.cn:8080/constellation/getAll?key=1e86cbe4ac7c8e1d23dcc5a133221f7b&type=today&consName=' + str).then(res => {
        fs.writeFileSync(path.join(__dirname, "./Json/constellation.json"), res.text)
        console.log(res)
    }).catch(
        console.error
    )
    // superagent
    // .post('http://web.juhe.cn:8080/constellation/getAll?key=1e86cbe4ac7c8e1d23dcc5a133221f7b&type=today&consName=' + str)
    // .send({ name: 'Manny', species: 'cat' }) // sends a JSON post body
    // .set('X-API-Key', 'foobar')
    // .set('accept', 'json')
    // .end((err, res) => {
    //     console.log(res)
    //     console.log(err)
    //     // Calling the end function will send the request
    // });
}
if (program.save) {
    console.log('保存文件')
}
if (program.find) {
    const promptList = [{
        type: 'input',
        message: '请输入1-12之间的月份:',
        name: 'moonth',
        default:'1 -- 12'
    }, {
        type: 'input',
        message: '请输入1-31之间的日期:',
        name: 'day',
        default:'1 -- 31'
    }]
    inquirer.prompt(promptList).then(answers => {
        console.log(answers)
        superagent.post(`http://api.juheapi.com/japi/toh?key=70f6b825f8356b64c8dfb33438c0fdd1&v=1.0&month=${answers.moonth}&day=${answers.day}`)
        .then(res => {
            console.log(JSON.parse(res.text))
        }).catch(console.error)
    }).catch(error => {
        if (error.isTtyError) {

        } else {

        }
    })
}